home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / collectn / checkeds.jav < prev    next >
Encoding:
Text File  |  1995-12-14  |  3.4 KB  |  142 lines

  1. /*
  2.   File: CheckedSet.java
  3.  
  4.   Originally written by Doug Lea and released into the public domain. 
  5.   Thanks for the assistance and support of Sun Microsystems Labs, Agorics 
  6.   Inc, Loral, and everyone contributing, testing, and using this code.
  7.  
  8.   History:
  9.   Date     Who                What
  10.   24Sep95  dl@cs.oswego.edu   Create from collections.java  working file
  11.   13Oct95  dl                 Misc clean-up.
  12.   19Oct95  dl                 More misc clean-up.
  13.  
  14. */
  15.   
  16. package collections;
  17.  
  18. import java.util.Enumeration;
  19. import java.util.NoSuchElementException;
  20.  
  21. /**
  22.  *
  23.  * @author Doug Lea
  24.  * @version 0.94
  25.  *
  26.  * <P> For an introduction to this package see <A HREF="index.html"> Overview </A>.
  27.  *
  28. **/
  29.  
  30. public class CheckedSet extends CheckedCollection implements UpdatableSet  {
  31.  
  32. /**
  33.  * Wrap Set s inside a checker
  34. **/
  35.   public CheckedSet(UpdatableSet s) { super(s); }
  36.  
  37. /**
  38.  * Make a Checked clone of underlying collection
  39. **/
  40.   protected Object clone() throws CloneNotSupportedException { 
  41.     return new CheckedSet((UpdatableSet)(thys.duplicate()));
  42.   }
  43.  
  44. /**
  45.  * return casted version of CheckedCollection.thys.
  46. **/
  47.   public UpdatableSet thys() { return (UpdatableSet)thys; }
  48.  
  49. /**
  50.  * return casted version of CheckedCollection.prev.
  51. **/
  52.   public UpdatableSet prev() { return (UpdatableSet)prev; }
  53.  
  54.  
  55.  
  56. /**
  57.  * Checks collections.UpdatableSet.include according to its specification
  58.  * @see collections.UpdatableSet#include
  59. **/
  60.   public synchronized void include(Object element) throws IllegalElementException {
  61.     preCheck();
  62.     try {
  63.       thys().include(element);
  64.       checkInclude(thys, prev, element, true);
  65.       postCheck();
  66.     }
  67.     catch (IllegalElementException ex) {
  68.       assert(!prev.canInclude(element));
  69.       assert(thys.sameStructure(prev));
  70.       postCheck();
  71.       throw ex;
  72.     }
  73.   }
  74.  
  75.  
  76.  
  77.  
  78. /**
  79.  * Checks collections.Set.including
  80.  * @see collections.Set#including
  81. **/
  82.   public synchronized  Set including(Object element) 
  83.   throws IllegalElementException {
  84.     preCheck();
  85.     try {
  86.       Set nc = thys().including(element);
  87.       checkInclude(nc, thys, element, false);
  88.       assert(thys.sameStructure(prev));
  89.       postCheck();
  90.       return nc;
  91.     }
  92.     catch (IllegalElementException ex) {
  93.       assert(!prev.canInclude(element));
  94.       assert(thys.sameStructure(prev));
  95.       postCheck();
  96.       throw ex;
  97.     }
  98.  
  99.   }
  100.  
  101. /**
  102.  * Checks collections.UpdatableSet.includeElements
  103.  * @see collections.UpdatableSet#includeElements
  104. **/
  105.  
  106.   public synchronized  void includeElements(Enumeration e) 
  107.    throws IllegalElementException, CorruptedEnumerationException {
  108.     preCheck();
  109.     thys().includeElements(e);
  110.     // can't check for elements because e is exhausted
  111.     postCheck();
  112.   }
  113.  
  114. /**
  115.  * Helper for checking includ*
  116. **/
  117.  
  118.   protected void checkInclude(Collection nc, Collection oc,
  119.                               Object element, boolean verchk) {
  120.  
  121.     assert(nc.canInclude(element));
  122.  
  123.     assert(nc.occurrencesOf(element) == 1);
  124.  
  125.     if (verchk) {
  126.       int nv = ((UpdatableCollection)nc).version();
  127.       assert((nv == prevVersion) == oc.includes(element));
  128.     }
  129.  
  130.     // all other elements the same
  131.     CollectionEnumeration os = oc.elements();
  132.     while (os.hasMoreElements()) {
  133.       Object v = os.nextElement();
  134.       assert(nc.occurrencesOf(v) == 1);
  135.     }
  136.     nc.checkImplementation();
  137.  
  138.   }
  139.  
  140. };
  141.  
  142.